package action.agent;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import security.MySecurityContextHandle;
import service.interf.IIfwAgentConsumeRecord;
import action.common.MyBaseAction;

import com.opensymphony.xwork2.ModelDriven;
import common.impl.exception.ServiceException;

import dao.pojo.IfwAgentConsumeRecord;

public class IfwAgentConsumeAction extends MyBaseAction<IfwAgentConsumeRecord>
		implements ModelDriven<IfwAgentConsumeRecord> {

	private static final long serialVersionUID = 1L;

	private IIfwAgentConsumeRecord iIfwAgentConsume;
	private Integer agentIndex;
	private String startDate;
	private String endDate;

	public void setiIfwAgentConsume(IIfwAgentConsumeRecord iIfwAgentConsume) {
		this.iIfwAgentConsume = iIfwAgentConsume;
	}

	public void setAgentIndex(Integer agentIndex) {
		this.agentIndex = agentIndex;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public IfwAgentConsumeAction() {
		super(IfwAgentConsumeRecord.class);
	}

	public IfwAgentConsumeRecord getModel() {
		return model;
	}

	/**
	 * 初始化列表
	 */
	public void getlist() {
		String x = "";
		try {
			String userName = MySecurityContextHandle.getUserName();
			String name;
			Object obj;
			if(null != agentIndex) {
				name = "ifwAgentRegInfo.agentIndex";
				obj = agentIndex;
			} else {
				name = "ifwAgentRegInfo.agentName";
				obj = userName;
			}
			List<String> pNameL = new ArrayList<String>();
			List<Object> pValueL = new ArrayList<Object>();
			List<String> relationL = new ArrayList<String>();
			List<String> logicL = new ArrayList<String>();
			// 开始时间
			if (!StringUtils.isEmpty(startDate)) {
				pNameL.add("ctime");
				pValueL.add(startDate);
				relationL.add(">=");
			}
			// 结束时间
			if (!StringUtils.isEmpty(endDate)) {
				pNameL.add("ctime");
				pValueL.add(endDate);
				relationL.add("<=");
			}
			String fields = "*,ifwAgentRegInfo.agentName(agentName),ifwAgentDevInfo.sn(sn),ifwCardInfo.cardIndex(cardIndex),ifwCardInfo.cardNum(cardNum),ifwCardInfo.cardSta(cardSta)";
			int type = 0;
			if (null != model.getType())
				type = model.getType().intValue();
			if (type == 2 || type == 1) {
				pNameL.add(name);
				pValueL.add(obj);
				relationL.add("=");
				pNameL.add("type");
				pValueL.add(type);
				relationL.add("=");
			} else {
				if (null == agentIndex && "admin".equals(userName)) {
					if (type == 3) {
						pNameL.add("type");
						pValueL.add(type);
						relationL.add("=");
					} else {
						pNameL.add("ifwAgentRegInfo.agentName");
						pValueL.add(userName);
						relationL.add("=");
						pNameL.add("type");
						pValueL.add(3);
						relationL.add("=");
						int size = pNameL.size() - 2;
						if (size > 0) {
							for (int i = 0; i < size; i++)
								logicL.add("and");
							logicL.add("(");
							logicL.add("or");
							logicL.add(")");
						} else {
							logicL.add("or");
						}
					}
				} else {
					pNameL.add(name);
					pValueL.add(obj);
					relationL.add("=");
					pNameL.add("type");
					pValueL.add(3);
					relationL.add("!=");
				}
			}
			String[] pNames = new String[pNameL.size()];
			pNames = pNameL.toArray(pNames);
			Object[] pValues = new Object[pValueL.size()];
			pValues = pValueL.toArray(pValues);
			String[] relations = new String[relationL.size()];
			relations = relationL.toArray(relations);
			String[] logics = new String[logicL.size()];
			logics = logicL.toArray(logics);
//			x = iIfwAgentConsume.findByProperty("IfwAgentConsumeRecord",
//					fields, "json", true, pNames, pValues, logics, relations,
//					model.getPage(), model.getRows(), new String[] { "ctime",
//							"desc" });
			x = iIfwAgentConsume.getList(pNames, pValues, logics, relations,
			model.getPage(), model.getRows());
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient(ServletActionContext.getResponse(), x);
	}

	/**
	 * 添加消费记录
	 */
	public void addConsume() {
		String x = "";
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String sessionVerifyCode = request.getParameter("vcode");
			if (null == sessionVerifyCode
					|| !common.impl.VerifyCode.getInstance().check(request,
							sessionVerifyCode)) {
				throw new ServiceException("验证码错误");
			}
			x = iIfwAgentConsume.addConsume(model);
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), x);
	}

	/**
	 * 购买点卡
	 */
	public void addCard() {
		String x = "";
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String sessionVerifyCode = request.getParameter("vcode");
			if (null == sessionVerifyCode
					|| !common.impl.VerifyCode.getInstance().check(request,
							sessionVerifyCode)) {
				throw new ServiceException("验证码错误");
			}
			x = iIfwAgentConsume.addCard(model);
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), x);
	}

	/**
	 * 代理商充值
	 */
	public void agentRecharge() {
		String x = "";
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String sessionVerifyCode = request.getParameter("vcode");
			if (null == sessionVerifyCode
					|| !common.impl.VerifyCode.getInstance().check(request,
							sessionVerifyCode)) {
				throw new ServiceException("验证码错误");
			}
			x = iIfwAgentConsume.agentRecharge(agentIndex, model.getPoint());
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), x);
	}
	
	/**
	 * 阶段共计消费
	 */
	public void gettotal() {
		String x = "";
		try {
			x = iIfwAgentConsume.getTotal(agentIndex, model.getType(), startDate, endDate);
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), x);
	}
}
